package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.SmsHomeBrand;
import com.itheima.mapper.SmsHomeBrandMapper;
import com.itheima.service.db.SmsHomeBrandService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeBrandServiceImpl implements SmsHomeBrandService {

    @Autowired
    SmsHomeBrandMapper smsHomeBrandMapper;

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, String brandName, Integer recommendStatus) {
        Page<SmsHomeBrand> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SmsHomeBrand> qw = new LambdaQueryWrapper<>();

        qw.like(StrUtil.isNotEmpty(brandName), SmsHomeBrand::getBrandName, brandName);
        qw.eq(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus);
        qw.orderByDesc(SmsHomeBrand::getSort);

        Page<SmsHomeBrand> smsHomeBrandPage = smsHomeBrandMapper.selectPage(page, qw);

        return new PageBeanVo(pageNum, pageSize, smsHomeBrandPage.getTotal(), smsHomeBrandPage.getRecords());
    }

    @Override
    public void updateRecommendStatus(Long id, Integer recommendStatus) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        smsHomeBrand.setRecommendStatus(recommendStatus);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }

    @Override
    public Integer create(List<SmsHomeBrand> smsHomeBrand) {
        if (CollUtil.isNotEmpty(smsHomeBrand)) {
            for (SmsHomeBrand homeBrand : smsHomeBrand) {
                smsHomeBrandMapper.insert(homeBrand);
            }
            return 1;
        }
        return null;

    }

    @Override
    public Integer delete(List<Integer> ids) {
        return (smsHomeBrandMapper.deleteBatchIds(ids));


    }

    @Override
    public Integer updateSortById(Integer id, Integer sort) {
        LambdaQueryWrapper<SmsHomeBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(SmsHomeBrand::getId, id);
        //根据id查询出smsHomeBrand数据
        SmsHomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        if (smsHomeBrand != null && sort != null) {
            smsHomeBrand.setSort(sort);
            return smsHomeBrandMapper.updateById(smsHomeBrand);
        }
        return null;
    }
}
